//
//  ViewController.m
//  class2
//
//  Created by Mac OS on 16-4-20.
//  Copyright (c) 2016年 lms. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController () <UITableViewDataSource>
{

    NSArray *data;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
   // NSLog(@"视图已经加载");
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource: @"tel" withExtension:@""];
    NSString *text = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：%@",text);
    
//解析数据
    NSArray  *arr = [text componentsSeparatedByString:@"\n"];
    NSMutableArray *tmp = [NSMutableArray new];
    //元素可变  NSMutableArray是NSArray的子类
    for (int i=0;i<arr.count;i++){
        //      换行

        NSString *item = arr[i];
        NSArray *p =[item componentsSeparatedByString:@","];
        Person *per = [Person new];
        per.name = p[0];
        per.tel = p[1];
        [tmp addObject:per];
    }
    data = tmp;
   }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return data.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    Person *p = data[indexPath.row];
    cell.textLabel.text = p.name;
    cell.detailTextLabel.text = p.tel;
    return cell;
    
    //Value1 detail在右边
    //Subtitle detail在下面
    //Default 无detail
    //Value2 detail与上课 下课 睡觉 在一起
}


//一个生命周期（加载，将要显示，已经显示）
//- (void)viewWillAppear:(BOOL)animated
//{
//NSLog(@"视图将要显示");
//    
//}
//
//- (void)viewDidAppear:(BOOL)animated
//{
//
//NSLog(@"视图已经显示");
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
